package LC219;

import java.util.HashMap;

public class Solution {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 1, 2, 3};
        int k = 2;
        Solution solution = new Solution();
        System.out.println(solution.findArrayInt(nums, k));
    }

    public boolean findArrayInt(int[] nums, int k) {
        // 使用HashMap来存储每个元素最近出现的索引
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(nums[i])) {
                // 如果当前元素之前出现过，检查索引差是否小于等于k
                int prevIndex = map.get(nums[i]);
                if (i - prevIndex <= k) {
                    return true;
                }
            }
            // 更新当前元素的最新索引
            map.put(nums[i], i);
        }
        return false;
    }
}
